package com.pi.dao;

import com.pi.po.ReportPO;
import com.pi.util.MySql;

import java.sql.*;
import java.util.*;

public class PublishDao {

    public boolean insertData(Map<String,String[]> map) throws SQLException {
        boolean success=false;
        Connection connection=MySql.getConnection();
        String sql="insert into report(";
        Set<String> keys=map.keySet();
        String sqlV="values(";
        for(String key : keys){
            sql+=key+",";
            if("report_column_number".equals(key)){
                sqlV+=map.get(key)[0]+",";
            }else {
                sqlV+="'"+map.get(key)[0]+"',";
            }
        }
        sqlV=sqlV.substring(0,sqlV.lastIndexOf(","));
        sqlV+=")";
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") "+sqlV;

        System.out.println("Sql:"+sql);
        Statement statement=connection.createStatement();
        if(statement.executeUpdate(sql)!=0){
            success=true;
        }
        return success;
    }

    public List<ReportPO> selectData() throws SQLException {
        List<ReportPO> reports=new ArrayList<ReportPO>();
        Connection connection= MySql.getConnection();
        Statement statement=connection.createStatement();
        ResultSet resultSet=statement.executeQuery("SELECT * FROM report");
        while(resultSet.next()){
            ReportPO report=new ReportPO();
            report.setReportId(Integer.parseInt(resultSet.getString("report_id")));
            report.setUserId(resultSet.getString("user_id"));
            report.setUserReportId(resultSet.getString("user_report_id"));
            report.setReportColumnNumber(Integer.parseInt(resultSet.getString("report_column_number")));
            String t;
            if((t=resultSet.getString("t1"))!=null){
                report.setT1(t);
            }
            if((t=resultSet.getString("t2"))!=null){
                report.setT2(t);
            }
            if((t=resultSet.getString("t3"))!=null){
                report.setT3(t);
            }
            if((t=resultSet.getString("t4"))!=null){
                report.setT4(t);
            }
            if((t=resultSet.getString("t5"))!=null){
                report.setT5(t);
            }
            if((t=resultSet.getString("t6"))!=null){
                report.setT6(t);
            }
            if((t=resultSet.getString("t7"))!=null){
                report.setT7(t);
            }
            if((t=resultSet.getString("t8"))!=null){
                report.setT8(t);
            }
            if((t=resultSet.getString("t9"))!=null){
                report.setT9(t);
            }
            if((t=resultSet.getString("t10"))!=null){
                report.setT10(t);
            }
            if((t=resultSet.getString("t11"))!=null){
                report.setT11(t);
            }
            if((t=resultSet.getString("t12"))!=null){
                report.setT12(t);
            }
            if((t=resultSet.getString("t13"))!=null){
                report.setT13(t);
            }
            if((t=resultSet.getString("t14"))!=null){
                report.setT14(t);
            }
            if((t=resultSet.getString("t15"))!=null){
                report.setT15(t);
            }
            if((t=resultSet.getString("t16"))!=null){
                report.setT16(t);
            }
            if((t=resultSet.getString("t17"))!=null){
                report.setT17(t);
            }
            if((t=resultSet.getString("t18"))!=null){
                report.setT18(t);
            }
            if((t=resultSet.getString("t19"))!=null){
                report.setT19(t);
            }
            if((t=resultSet.getString("t20"))!=null){
                report.setT20(t);
            }
            if((t=resultSet.getString("t21"))!=null){
                report.setT21(t);
            }
            if((t=resultSet.getString("t22"))!=null){
                report.setT22(t);
            }
            reports.add(report);
        }
        return reports;
    }

}
